package com.mou.controller.admin;


import com.mou.dto.RoleDTO;
import com.mou.dto.RolePageQueryDTO;
import com.mou.entity.Role;
import com.mou.result.PageResult;
import com.mou.result.Result;
import com.mou.service.RoleService;
import com.mou.vo.RoleVO;
import com.mou.vo.SelectRoleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin/role")
public class RoleController {

    @Autowired
    private RoleService roleService;


    @GetMapping("/page")
    public Result<PageResult> page(RolePageQueryDTO rolePageQueryDTO){
        log.info("角色分页查询:{}", rolePageQueryDTO);
        PageResult pageResult = roleService.pageQuery(rolePageQueryDTO);
        return Result.success(pageResult);
    }


    @PostMapping("/add")
    public Result<String> add(@RequestBody RoleDTO roleDTO){
        log.info("角色添加:{}", roleDTO);
        Role role = new Role();
        BeanUtils.copyProperties(roleDTO,role);
        LocalDateTime now = LocalDateTime.now();
        role.setCreateTime(now);
        role.setUpdateTime(now);
        roleService.save(role);
        return Result.success();
    }

    @PutMapping("/update")
    public Result<String> update(@RequestBody RoleDTO roleDTO){
        log.info("角色修改:{}", roleDTO);
        Role role = new Role();
        BeanUtils.copyProperties(roleDTO,role);
        LocalDateTime now = LocalDateTime.now();
        role.setUpdateTime(now);
        roleService.updateById(role);
        return Result.success();
    }

    @PutMapping("/delete/{id}")
    public Result<String> delete(@PathVariable("id") Integer id){
        log.info("角色删除:{}", id);
        roleService.delete(id);
        return Result.success();
    }

    @GetMapping("/getSelectRole")
    public Result<List<SelectRoleVO>> getAllRole(){
        log.info("获取所有选择角色");
        List<SelectRoleVO> selectRole = roleService.getSelectRole();
        return Result.success(selectRole);
    }

}
